<?php  
session_start();

require_once('uploadHandling.php');
require_once('ping.php');

$bResult = -1;
if (isset($_FILES['patch'])) {
	$bResult = handleUpload($_FILES);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
        <script type="text/javascript">
			function bodyload() {
				$.get("parts/login.part.php", login_cb);
				updatePatches();
				updateResults();
				updateOutput();
			}

			function login() {
				$.post("helpers/doLogIn.php", { username: $("#username").val(), password: $("#password").val() } );
				$.get("parts/login.part.php", login_cb);
			}

			function login_cb(data) {
				$("#divlogin").html(data);
			}

			 function updatePatches() {
				 $.get("helpers/getPatchQueue.php", updatePatches_cb);
			 }

			 function updatePatches_cb(data){
				 var patches = eval("("+data+")");

				 $("#patchqueue").html("");

				 // Empty list
				 if (patches.length < 1) {
					$("#patchqueue").append("<p>No patches in queue!</p>");
					return;
				 }

				 if ("running" in patches) {
					 $('#patchqueue').append("<p>"+patches["running"]+" (running)</p>");
				 }
				 
				 for (var k in patches){
					 	if (k == "running") continue;
						$('#patchqueue').append("<p>"+patches[k]+"</p>");
				 }
			 }

			 function updateResults() {
				 $.get("helpers/getResultsQueue.php", updateResults_cb);
			 }

			 function updateResults_cb(data){
				 var results = eval("("+data+")");

				 $("#resultsqueue").html("");

				 // Empty list
				 if (results.length < 1) {
					$("#resultsqueue").append("<p>No results to display!</p>");
					return;
				 }

				 for (var k in results){
						$('#resultsqueue').append("<p><a href=\""+results[k]+"\">"+k+"</a></p>");
				 }
			 }

			 function updateOutput(){
				 $.get("helpers/getTestOutput.php", updateOutput_cb);
			 }

			 function updateOutput_cb(data){

				 $("#outputframe").html("");
				 
				 if (data.length == 0){
					 $("#outputframe").append("<p>No output to display!</p>");
						return;
				 }

				 $('#outputframe').append(data);
			 }
		</script>
        <title>Derby Regressions Server</title>
</head>
<body onload="bodyload()">
<h1>Derby Regressions Server</h1>
<?php 
if ($bResult === true) {
	echo "Upload was successful!";
} else if($bResult === false) {
	echo "Upload was unsuccessful!";
}
?>
<div id="divlogin">

</div>

<div id="divsubmit">
<form method="post" enctype="multipart/form-data" action="index.php">
<h3>Submit new patch</h3>
<div id="patchsubmit">
<input type="file" name="patch" /><br />
<input type="submit" value="Submit" />
</div>
</form>
</div>

<div id="divqueue">
<h3>Patch Queue</h3>
<div id="patchqueue">
<p>No patches in queue!</p>
</div>
<a href="#" onclick="updatePatches()"><img id="patchrefresh" src="img/refresh-icon.png" alt="" /></a>
</div>

<div id="divresults">
<h3>Test Results</h3>
<div id="resultsqueue">
<p>No results to display!</p>
</div>
<a href="#" onclick="updateResults()"><img id="patchrefresh" src="img/refresh-icon.png" alt="" /></a>
</div>

<div id="divoutput">
<h3>Test Output</h3>
<div id="outputframe">
<p>No output to display!</p>
</div>
<a href="#" onclick="updateOutput()"><img id="patchrefresh" src="img/refresh-icon.png" alt="" /></a>
</div>
</body>
</html>